// pages/userRegister/userRegister.js
let Mcaptcha = require('../../utils/mcaptcha.js');
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '注册',
    codeInputValue: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  // 获取生成验证码
  getRanNum: function () {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var pwd = '';
    for (var i = 0; i < 4; i++) {
      if (Math.random() < 48) {
        pwd += chars.charAt(Math.random() * 48 - 1);
      }
    }
    return pwd;
  },
  // 监听phone值
  phoneInput(e) {
    let phoneInput = e.detail.value
    if (phoneInput == '') {
      wx.showToast({
        title: '输入框不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        phoneInput
      })
    }

  },
  // 邀请人号码
  phoneInvite(e) {
    let phoneInvite = e.detail.value
    this.setData({
      phoneInvite
    })
  },
  // 监听password值
  passwordInput(e) {
    let _this = this
    let passwordInput = e.detail.value
    let repeatPassword = _this.data.repeatPassword
    if (passwordInput == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        passwordInput
      })
    }

  },
  // 监听repeatPassword值
  repeatPassword(e) {
    let _this = this
    let repeatPassword = e.detail.value
    let passwordInput = _this.data.passwordInput

    if (passwordInput !== repeatPassword) {
      wx.showToast({
        title: '密码不一致',
        icon: "none"
      })
    } else if (repeatPassword == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: "none"
      })
    } else {
      this.setData({
        repeatPassword
      })
    }

  },
  // 监听code值
  codeInput(e) {
    let _this = this
    let codeInputValue = e.detail.value;
    if (codeInputValue == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: "none"
      })
    } else {
      this.setData({
        codeInputValue
      })
    }
  },
  // 注册提交
  onRegister(e) {
    let _this = this
    let phone = _this.data.phoneInput
    let password = _this.data.passwordInput
    let passwords = _this.data.repeatPassword
    let code = _this.data.num
    let phoneInvite = _this.data.phoneInvite
    let wxcode = wx.getStorageSync('code')



    //验证验证码是否正确以及请求
    let check_Code = (_this.data.num).toLowerCase()
    let check_Value = (_this.data.codeInputValue).toLowerCase()
    if (check_Value == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
    } else if (check_Code != check_Value) {
      wx.showToast({
        title: '验证码错误',
        icon: 'none'
      })
    } else {
      let _this = this
      wx.getUserInfo({
        complete: (res) => {
          this.setData({
            avatarUrl: res.userInfo.avatarUrl,
            nickName: res.userInfo.nickName
          })
          console.log(res.userInfo.avatarUrl, 'avatarUrl')
          console.log(res.userInfo.nickName, 'nickName')
        },
      })
      /**
       * @param {number} phone 手机号码
       * @param {number} password 密码
       * @param {number} passwords 重复密码
       * @param {number} code 验证码
       * @param {string} wx_code 微信code
       */

      http.postHttp('/user_registe', {
        phone: phone,
        password: password,
        passwords: passwords,
        code: code,
        wx_code: wxcode,
        invite: phoneInvite,
        avatar: _this.data.avatarUrl,
        nickname: _this.data.nickName
      }, {}).then(res => {
        // console.log(res.data.msg, 'msg')
        // console.log(res, 'resgister')
        if (res.data.code == 1) {
          
          wx.setStorageSync('userid', res.data.data.userinfo.user_id)
          wx.setStorageSync('token', res.data.data.userinfo.token)
          wx.showToast({
            title: '注册成功',
            icon: 'none'
          })
          wx.navigateTo({
            url: '/pages/myIndex/myIndex'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    }


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.login({
      complete: (res) => {
        wx.setStorageSync('code', res.code)
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    var num = that.getRanNum();
    console.log(num)
    this.setData({
      num: num
    })
    new Mcaptcha({
      el: 'canvas',
      width: 80, //对图形的宽高进行控制
      height: 30,
      code: num
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})